package com.group10.service.impl;

import com.group10.entity.Admin;
import com.group10.entity.Result;
import com.group10.entity.User;
import com.group10.entity.UserState;
import com.group10.mapper.AdminMapper;
import com.group10.service.AdminService;
import com.group10.util.Encrypt;
import com.group10.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result<?> adminLogin(String id, String password) {
        Admin a = adminMapper.findAdmin(id);
        if(a != null){
            String str = Encrypt.toMD5(password);
            if(a.getPassword().equals(str)){
                // 登录成功
                Map<String,Object> claims = new HashMap<>();
                claims.put("id",a.getId());
                claims.put("name",a.getName());
                String token = JwtUtil.genToken(claims);
                return Result.success(token);
            }else{
                return Result.error("密码错误");
            }
        }else{
            return Result.error("不存在的账号");
        }
    }
}
